//
//  QQLoginViewController.m
//  QQLibDemo
//
//  Created by mac on 16/12/13.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "QQLoginViewController.h"
#import "WeChatQQStaticLibHeader.h"

@interface QQLoginViewController ()
{
    NSString * m_vcode;
    NSString * m_vcode_sig;
    NSString * m_uin;
    UIColor * m_mainColor;
}
@end

@implementation QQLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(onDismissViewController)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    self.m_text_passport.text = [[WeChatQQApi stander] cachePassport];
    self.m_text_password.text = [[WeChatQQApi stander] cachePassword];

    self.m_backView_imageCodeBack.hidden = YES;
    
    CREATE_BORDER_EXTEND(self.m_view_imageCode_back,5, 0.5, 0, 0, 0, 0.2);
    CREATE_BORDER_EXTEND(self.m_label_imageCode_back,5, 0.5, 0, 0, 0, 0.2);
    
    CREATE_BORDER_EXTEND(self.m_label_passport_password_back, 5, 1, 217.0, 217.0, 217.0, 1);
    CREATE_BORDER_EXTEND(self.m_btn_login, 5, 1, 116.0, 192.0, 54.0, 1 );
    CREATE_BORDER_EXTEND(self.m_label_user_info_back, 5, 1, 217.0, 217.0, 217.0, 1);
    CREATE_BORDER_EXTEND(self.m_btn_login_for_image_code, 5, 1, 116.0, 192.0, 54.0, 1 );
    
    self.m_label_passport_password_back.backgroundColor = [UIColor clearColor];
    self.m_label_user_info_back.backgroundColor = [UIColor clearColor];
    
    self.m_image_code.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSetImageCodeBtn:)];
    [self.m_image_code addGestureRecognizer:singleTap];
    
    //[self.m_btn_login setBackgroundColor:m_mainColor];
    //[self.m_btn_login_for_image_code setBackgroundColor:m_mainColor];
    
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.barTintColor = HEXCOLOR(0x1496DB);
    bar.tintColor = [UIColor whiteColor];
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)onCloseImageCode:(id)sender {
    m_vcode = nil;
    self.m_backView_imageCodeBack.hidden = YES;
}

- (IBAction)onLoginBtn:(id)sender {
    if (_m_text_passport.text.length <= 0 || _m_text_password.text.length <= 0) {
        return;
    }
    
    [_m_text_passport isFirstResponder];
    [_m_text_password isFirstResponder];
    [_m_text_image isFirstResponder];
    
    if ([WeChatQQApi stander].m_isLogin) {
        NSLog(@"已经自动登录成功!");
        [self onDismissViewController];
        return;
    }
    
    SHOW_HUD(@"登陆中...");
    QQAsyncSimulationService * service = [QQAsyncSimulationService stander];
    [service asyncLogin:_m_text_passport.text password:_m_text_password.text imageCode:_m_text_image.text showImageCodeBlock:^(UIImage *imageCode) {
        HIDE_HUD;
        
        [self.m_image_code setImage:imageCode];
        self.m_backView_imageCodeBack.hidden = NO;
        self.m_view_imageCode_back.backgroundColor = [UIColor darkGrayColor];
        
        [self.m_text_image becomeFirstResponder];
    } respBlock:^(WQHttpResult *result) {
        HIDE_HUD;
        
        if (result.ret == 0) {
            NSLog(@"登录成功!");
            [self onDismissViewController];
        }
        else{
            NSLog(@"%@",result.msg);
            SHOW_HUD_MESSAGE(result.msg);
        }
    }];
}

- (IBAction)onShowAllUserInfo:(id)sender {
    self.m_btn_show_all_user_info.hidden = YES;
    self.m_image_show_all_user_info.hidden = YES;
    self.m_label_show_all_user_info.hidden = YES;
    
    self.m_btn_hide_all_user_info.hidden = NO;
    self.m_image_hide_all_user_info.hidden = NO;
    self.m_label_hide_all_user_info.hidden = NO;
    
    self.m_image_user_info_1.hidden = NO;
    self.m_image_user_info_2.hidden = NO;
    self.m_image_user_info_3.hidden = NO;
    self.m_image_user_info_4.hidden = NO;
    self.m_image_user_info_5.hidden = NO;
    
    self.m_label_user_info_1.hidden = NO;
    self.m_label_user_info_2.hidden = NO;
    self.m_label_user_info_3.hidden = NO;
    self.m_label_user_info_4.hidden = NO;
    self.m_label_user_info_5.hidden = NO;
    
    self.m_label_user_info_border_1.hidden = NO;
    self.m_label_user_info_border_2.hidden = NO;
    self.m_label_user_info_border_3.hidden = NO;
    self.m_label_user_info_border_4.hidden = NO;
    self.m_label_user_info_border_5.hidden = NO;
    
    self.m_btn_user_info_1.hidden = NO;
    self.m_btn_user_info_2.hidden = NO;
    self.m_btn_user_info_3.hidden = NO;
    self.m_btn_user_info_4.hidden = NO;
    self.m_btn_user_info_5.hidden = NO;
    
    self.m_btn_user_info_1.selected = YES;
    self.m_btn_user_info_2.selected = YES;
    self.m_btn_user_info_3.selected = YES;
    self.m_btn_user_info_4.selected = YES;
    self.m_btn_user_info_5.selected = YES;
    
    CGRect rect = self.m_label_user_info_back.frame;
    rect.size.height = 41.0 * 8;
    [self.m_label_user_info_back setFrame:rect];
    
    CGSize contentSize = self.m_scrollview_back.frame.size;
    contentSize.height = rect.origin.y + rect.size.height + 50.0f;
    self.m_scrollview_back.contentSize = contentSize;
    
    CGRect copyRightRect = self.m_label_copyright.frame;
    copyRightRect.origin.y = rect.origin.y + rect.size.height + 20.0f;
    self.m_label_copyright.frame = copyRightRect;
}

- (IBAction)onHideAllUserInfo:(id)sender {
    self.m_btn_show_all_user_info.hidden = NO;
    self.m_image_show_all_user_info.hidden = NO;
    self.m_label_show_all_user_info.hidden = NO;
    
    self.m_btn_hide_all_user_info.hidden = YES;
    self.m_image_hide_all_user_info.hidden = YES;
    self.m_label_hide_all_user_info.hidden = YES;
    
    self.m_image_user_info_1.hidden = YES;
    self.m_image_user_info_2.hidden = YES;
    self.m_image_user_info_3.hidden = YES;
    self.m_image_user_info_4.hidden = YES;
    self.m_image_user_info_5.hidden = YES;
    
    self.m_label_user_info_1.hidden = YES;
    self.m_label_user_info_2.hidden = YES;
    self.m_label_user_info_3.hidden = YES;
    self.m_label_user_info_4.hidden = YES;
    self.m_label_user_info_5.hidden = YES;
    
    self.m_label_user_info_border_1.hidden = YES;
    self.m_label_user_info_border_2.hidden = YES;
    self.m_label_user_info_border_3.hidden = YES;
    self.m_label_user_info_border_4.hidden = YES;
    self.m_label_user_info_border_5.hidden = YES;
    
    self.m_btn_user_info_1.hidden = YES;
    self.m_btn_user_info_2.hidden = YES;
    self.m_btn_user_info_3.hidden = YES;
    self.m_btn_user_info_4.hidden = YES;
    self.m_btn_user_info_5.hidden = YES;
    
    CGRect rect = self.m_label_user_info_back.frame;
    rect.size.height = 41.0 * 3;
    [self.m_label_user_info_back setFrame:rect];
    
    CGSize contentSize = self.m_scrollview_back.frame.size;
    contentSize.height = rect.origin.y + rect.size.height + 50.0f;
    self.m_scrollview_back.contentSize = contentSize;
    
    CGRect copyRightRect = self.m_label_copyright.frame;
    copyRightRect.origin.y = rect.origin.y + rect.size.height + 20.0f;
    self.m_label_copyright.frame = copyRightRect;
}

- (IBAction)onUserInfoChange1:(UIButton *)sender {
    if (sender.isSelected) {
        self.m_image_user_info_1.image = [UIImage imageNamed:@"qq_chioc_nol.png"];
        sender.selected = NO;
    }else{
        self.m_image_user_info_1.image = [UIImage imageNamed:@"lu_gou.png"];
        sender.selected  = YES;
    }
}

- (IBAction)onUserInfoChange2:(UIButton *)sender {
    if (sender.isSelected) {
        self.m_image_user_info_2.image = [UIImage imageNamed:@"qq_chioc_nol.png"];
        sender.selected = NO;
    }else{
        self.m_image_user_info_2.image = [UIImage imageNamed:@"lu_gou.png"];
        sender.selected  = YES;
    }
}

- (IBAction)onUserInfoChange3:(UIButton *)sender {
    if (sender.isSelected) {
        self.m_image_user_info_3.image = [UIImage imageNamed:@"qq_chioc_nol.png"];
        sender.selected = NO;
    }else{
        self.m_image_user_info_3.image = [UIImage imageNamed:@"lu_gou.png"];
        sender.selected  = YES;
    }
}

- (IBAction)onUserInfoChange4:(UIButton *)sender {
    if (sender.isSelected) {
        self.m_image_user_info_4.image = [UIImage imageNamed:@"qq_chioc_nol.png"];
        sender.selected = NO;
    }else{
        self.m_image_user_info_4.image = [UIImage imageNamed:@"lu_gou.png"];
        sender.selected  = YES;
    }
}

- (IBAction)onUserInfoChange5:(UIButton *)sender {
    if (sender.isSelected) {
        self.m_image_user_info_5.image = [UIImage imageNamed:@"qq_chioc_nol.png"];
        sender.selected = NO;
    }else{
        self.m_image_user_info_5.image = [UIImage imageNamed:@"lu_gou.png"];
        sender.selected  = YES;
    }
}

- (void)updateMainColor:(UIColor *)color
{
    m_mainColor = color;
}

- (void)onSetImageCodeBtn:(id)sender {
    [[QQAsyncSimulationService stander]asyncGetImageCode:^(UIImage *imageCode) {
        [self.m_image_code setImage:imageCode];
    }];
}

- (void)onDismissViewController{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
